// import { lk_url1,lk_common,xc_url1,xc_url2,hhm_url1,hhm_url2,lk_url3  } from './myConfig.js';
import { lk_buyer,lk_common,lk_expert,lk_seller,lk_manager,lk_consumer } from './myConfig.js';
// 对网络请求重新封装，等待登陆后请求，防止token失败
function request(method, url, data, header) {
	return new Promise((resolve, reject) => {
		if (getApp().globalData._IsLogin_) {
			// console.warn("直接登陆："+url);
			requestX(method, url, data, header).then(function(data) {
				resolve(data);
			}).catch(function(data) {
				reject(data);
			})
		} else {
			// 设置定时器，等待登陆完成
			let cnt = 0;
			let tid = setInterval(() => {
				if (cnt > 2000) { //设置最大等待时间
					clearInterval(tid);
					reject("请求超时，请刷新重试！");
					return;
				}
				// console.warn("等待登陆完成："+(++cnt));
				if (getApp().globalData._IsLogin_) {
					// console.warn("等待登陆成功："+cnt+" ---"+url);
					clearInterval(tid);
					requestX(method, url, data, header).then(function(data) {
						resolve(data);
					}).catch(function(data) {
						reject(data);
					})
				}
			}, 10)
		}
	})
}

function requestX(method, url, data, header) {
	let headers = Object.assign({
		'content-type': 'application/json',
		'Authorization': uni.getStorageSync('accessToken'),
		'Authorization_jbs': uni.getStorageSync('accessToken')
	}, header)
	return new Promise((resolve, reject) => {
		// uni.hideLoading();
		uni.request({
			url,
			method,
			data,
			header: headers,
			success: ({
				data: res
			}) => {
				//  uni.hideLoading() 
				if (res && res.code.code === 200) {
					try {
						const _data_ = JSON.parse(res.data || '{}');
						resolve(_data_);
					} catch (error) {
						resolve(res.data);
					}
				} else {
					uni.showToast({
						title: res.message || '服务器响应超时,请稍后再试！',
						icon: 'none'
					});
					reject(res.message);
				}
			},
			fail: function(res) {
				console.log(res, 9999)
				uni.hideLoading();
				uni.showToast({
					title: res.message || '网络响应超时,请稍后再试！',
					icon: 'none'
				});
				reject(res.data);
			}
		});
	})
}

function requesthttpLogin(method, url, data, header) {
	let headers = Object.assign({
		'content-type': 'application/json',
	}, header)
	return new Promise((resolve, reject) => {
		uni.hideLoading();
		uni.request({
			url,
			method,
			data,
			header: headers,
			success: ({
				data: res
			}) => {
				uni.hideLoading();
				resolve(res);
			},
			fail: function(res) {
				uni.hideLoading();
				uni.showToast({
					title: res.message || '网络响应超时,请稍后再试！',
					icon: 'none'
				});
				reject(res);
			}
		});
	})
}

function requesthttp(method, url, data, header) {
	let headers = Object.assign({
		'content-type': 'application/json',
		'accessToken': uni.getStorageSync('accessToken'),
		'refreshToken': uni.getStorageSync('accessToken')
		// 'Authorization': uni.getStorageSync('token'),
		// 'Authorization_jbs': uni.getStorageSync('token'),
		// 'accessToken':'eyJhbGciOiJIUzI1NiJ9.eyJ1c2VyQ29udGV4dCI6IntcInVzZXJuYW1lXCI6XCIxODMyODAzNTAxM1wiLFwibmlja05hbWVcIjpcIuadjuaYjlwiLFwiZmFjZVwiOlwiaHR0cDovLzE0MC4yMTAuMjAxLjE1ODo5MDExL28yby83M2ZjNmM3MzQ5ZGU0OTRkYjFmYzBlNjY5ZDg1MmU3ZS5wbmdcIixcImlkXCI6XCIxNTc4MjA5MzQ1MjMyNTMxNDU2XCIsXCJsb25nVGVybVwiOmZhbHNlLFwicm9sZVwiOlwiTUVNQkVSXCIsXCJpc1N1cGVyXCI6ZmFsc2V9Iiwic3ViIjoiMTgzMjgwMzUwMTMiLCJleHAiOjE2NjkzNTMwMzB9.cR5x02w6n-e7lQhuC0s9-3FG28JBATGVvNrr9PtuzE0'
	}, header)
	return new Promise((resolve, reject) => {
		uni.hideLoading();
		uni.request({
			url,
			method,
			data,
			header: headers,
			success: ({
				data: res
			}) => {
				uni.hideLoading();
				resolve(res);
			},
			fail: function(res) {
				uni.hideLoading();
				uni.showToast({
					title: res.message || '网络响应超时,请稍后再试！',
					icon: 'none'
				});
				reject(res);
			}
		});
	})
}

function requestlist(method, url, data, header) {
	let headers = Object.assign({
		'content-type': 'application/json',
		'Authorization': uni.getStorageSync('accessToken'),
		'Authorization_jbs': uni.getStorageSync('accessToken')
	}, header)
	return new Promise((resolve, reject) => {
		if (getApp().globalData._IsLogin_) {
			// console.warn("直接登陆："+url);
			requestlistX(method, url, data, header).then(function(data) {
				resolve(data);
			}).catch(function(data) {
				reject(data);
			})
		} else {
			// 设置定时器，等待登陆完成
			let cnt = 0;
			let tid = setInterval(() => {
				if (cnt > 500) { //设置最大等待时间
					clearInterval(tid);
					reject("请求超时，请刷新重试！");
					return;
				}
				// console.warn("等待登陆完成："+(++cnt));
				if (getApp().globalData._IsLogin_) {
					// console.warn("等待登陆成功："+cnt+" ---"+url);
					clearInterval(tid);
					requestlistX(method, url, data, header).then(function(data) {
						resolve(data);
					}).catch(function(data) {
						reject(data);
					})
				}
			}, 10)
		}
	})
}

function requestlistX(method, url, data, header) {
	let headers = Object.assign({
		'content-type': 'application/json',
		'Authorization': uni.getStorageSync('accessToken'),
		'Authorization_jbs': uni.getStorageSync('accessToken')
	}, header)
	return new Promise((resolve, reject) => {
		// uni.hideLoading();
		uni.request({
			url,
			method,
			data,
			header: headers,
			success(res) {
				// uni.hideLoading();
				resolve(res.data);
			},
			fail: function(res) {
				// uni.hideLoading();
				uni.showToast({
					title: res.message || '网络响应超时,请稍后再试！',
					icon: 'none'
				});
				reject(res);
			}
		});
	})
}

function requestRight(method, url, data, header) {
	let headers = Object.assign({
		'content-type': 'application/json',
		'Authorization': uni.getStorageSync('accessToken'),
		'Authorization_jbs': uni.getStorageSync('accessToken'),
		'accessToken': null,
	}, header)
	return new Promise((resolve, reject) => {
		// uni.hideLoading();
		uni.request({
			url: url,
			method,
			header: headers,
			success(res) {
				// uni.hideLoading();
				resolve(res.data);
			},
			fail: function(res) {
				// uni.hideLoading();
				uni.showToast({
					title: res.message || '网络响应超时,请稍后再试！',
					icon: 'none'
				});
				reject(res);
			}
		});
	})
}

export const lyyApi = {
	
		
		// 用户咨询管理接口
		/**
		 * @param {
			  "status": 0,
			  "userId": 0
			}
		 * @description 用户查询自己的咨询 
		 * @detail 
		 * @author lyy
		 */
		UserConsultDTO: (data) => requesthttp('POST', `${lk_seller}/userConsult/getListByUserConsultDTO`, data, {
			'content-type': 'application/json'
		}),
		/**
		 * @param {}
		 * @description 用户查询所有对象
		 * @detail 
		 * @author lyy
		 */
		userConsultList: (data) => requesthttp('GET', `${lk_seller}/userConsult/list`, data, {
			'content-type': 'application/x-www-form-urlencoded'
		}),
		/**
		 * @param {
			  "keyword": "",
			  "page": 0,
			  "rows": 0,
			  "sortField": "",
			  "sortType": ""
			}
		 * @description 带条件分页查询数据
		 * @detail 
		 * @author lyy
		 */
		userConsultPagelist: (data) => requesthttp('POST', `${lk_seller}/userConsult/pagelist`, data, {
			'content-type': 'application/json'
		}),
		/**
		 * @param {
			 ...,"member": {..., "mobile": "",...}
			}
		 * @description 用户新增咨询
		 * @detail 
		 * @author lyy
		 */
		userConsultSave: (data) => requesthttp('POST', `${lk_seller}/userConsult/save`, data, {
			'content-type': 'application/json'
		}),
		/**
		 * @param {
			 ...,"member": {..., "mobile": "",...}
			}
		 * @description 用户取消咨询
		 * @detail 
		 * @author lyy
		 */
		uc_updateStatus: (data) => requesthttp('POST', `${lk_seller}/userConsult/updateStatus`, data, {
			'content-type': 'application/json'
		}),
		/**
		 * @param {id}  integer(int64)
		 * @description 获取对象
		 * @detail 
		 * @author lyy
		 */
		userConsult: (id) => requesthttp('GET', `${lk_seller}/userConsult/${id}`, data, {
			'content-type': 'application/x-www-form-urlencoded'
		}),
		/**
		 * @param {id}  integer(int64)
		 * @description 删除对象
		 * @detail 
		 * @author lyy
		 */
		userConsult: (id) => requesthttp('DELETE', `${lk_seller}/userConsult/${id}`, data, {
			'content-type': 'application/x-www-form-urlencoded'
		}),
		//用户挂号预约管理接口
		/**
		 * @param {id}  integer(int64)
		 * @description 用户签到
		 * @detail 
		 * @author lyy
		 */
		ur_arrive: (id) => requesthttp('PUT', `${lk_seller}/userRegister/arrive/${id}`, data, {
			'content-type': 'application/x-www-form-urlencoded'
		}),
		/**
		 * @param {id}  integer(int64)
		 * @description 用户取消挂号预约
		 * @detail 
		 * @author lyy
		 */
		ur_cancel: (id) => requesthttp('PUT', `${lk_seller}/userRegister/cancel/${id}`, data, {
			'content-type': 'application/x-www-form-urlencoded'
		}),
		/**
		 * @param {expertId:"专家ID",timeSlot:"" }  //时段(0今日挂号，1预约挂号，2历史挂号)
		 * @description 专家分时段获取挂号集合
		 * @detail 
		 * @author lyy
		 */
		ur_expert: (expertId,timeSlot) => requesthttp('GET', `${lk_seller}/userRegister/expert/${expertId}/${timeSlot}`, data, {
			'content-type': 'application/x-www-form-urlencoded'
		}),
		/**
		 * @param { }
		 * @description 查询所有挂号预约
		 * @detail 
		 * @author lyy
		 */
		ur_list: (data) => requesthttp('GET', `${lk_seller}/userRegister/list`, data, {
			'content-type': 'application/x-www-form-urlencoded'
		}),
		/**
		 * @param {
		  "arrive": 0,
		  "expertId": 0,
		  "keyword": "",
		  "name": "",
		  "page": 0,
		  "phone": "",
		  "rows": 0,
		  "sortField": "",
		  "sortType": "",
		  "status": 0,
		  "userId": 0
		} // integer(int32)
		 * @description 分页条件查询
		 * @detail 
		 * @author lyy
		 */
		ur_pagelist: (data) => requesthttp('POST', `${lk_seller}/userRegister/pagelist`, data, {
			'content-type': 'application/json'
		}),
		
		/**
		 * @param {... }
		 * @description 用户新增挂号预约
		 * @detail 
		 * @author lyy
		 */
		ur_save: (data) => requesthttp('POST', `${lk_seller}/userRegister/save`, data, {
			'content-type': 'application/json'
		}),
		/**
		 * @param {ID } //	integer(int64)
		 * @description 根据ID获取挂号单
		 * @detail 
		 * @author lyy
		 */
		ur_id: (id,data) => requesthttp('GET', `${lk_seller}/userRegister/${id}`, data, {
			'content-type': 'application/x-www-form-urlencoded'
		}),
		/**
		 * @param {ID } //	integer(int64)
		 * @description 根据ID删除挂号预约
		 * @detail 
		 * @author lyy
		 */
		ur_deleteid: (id,data) => requesthttp('DELETE', `${lk_seller}/userRegister/${id}`, data, {
			'content-type': 'application/x-www-form-urlencoded'
		}),
	
		
		// 专家管理接口
		/**
		 * @param {... } 
		 * @description 获取专家排序的集合
		 * @detail 已联调
		 * @author lyy
		 */
		expertSortList: (data) => requesthttp('POST', `${lk_expert}/expert/expertSortList`, data, {
			'content-type': 'application/json'
		}),
		/**
		 * @param {ID } //	integer(int64)
		 * @description 根据ID获取专家信息
		 * @detail 已联调
		 * @author lyy
		 */
		expertData: (id,data) => requesthttp('GET', `${lk_expert}/expert/${id}`, data, {
			'content-type': 'application/x-www-form-urlencoded'
		}),
		/**
		 * @param {
		  "expertId": 0,
		  "keyword": "",
		  "page": 0,
		  "rows": 0,
		  "sortField": "",
		  "sortType": ""
		} // false
		 * @description 根据ID获取专家评论集合
		 * @detail 已联调
		 * @author lyy
		 */
		expertEvaluate: (data) => requesthttp('POST', `${lk_expert}/expert/getExpertEvaluate`, data, {
			'content-type': 'application/json'
		}),
		
		/**
		 * @param {
		  "expertId": 0,
		  "keyword": "",
		  "page": 0,
		  "rows": 0,
		  "sortField": "",
		  "sortType": "",
		  "type": 0
		} // false
		 * @description 根据ID获取查询指定专家的知识（健康科普）
		 * @detail 已联调
		 * @author lyy
		 */
		expertKnowledge: (data) => requesthttp('POST', `${lk_expert}/expert/expert/expert/knowledge`, data, {
			'content-type': 'application/json'
		}),
		//  资讯评论管理接口
		/**
		 * @param {informationId		} // integer(int64)
		 * @description 根据资讯ID获取资讯评论树
		 * @detail 已联调
		 * @author lyy
		 */
		informationComment: (param) => requesthttp('POST', `${lk_expert}/expert/information/informationComment/commentTree`, param, {
			'content-type': 'application/json'
		}),
		//  资讯评论管理接口
		/**
		 * @param {informationId		} // integer(int64)
		 * @description 回复评论
		 * @detail 已联调
		 * @author lyy
		 */
		 informationComment2: (data) => requesthttp('POST', `${lk_expert}/expert/information/informationComment/saveComment`, data, {
			'content-type': 'application/json'
		}),
		/**
		 * @param {informationId		} // integer(int64)
		 * @description 评论新增点赞
		 * @detail 已联调
		 * @author lyy
		 */
		informationCommentLike: (id,data) => requesthttp('GET', `${lk_expert}/expert/information/informationComment/like/${id}`, data, {
			'content-type': 'application/x-www-form-urlencoded'
		}),
		
		
		// 买家用户信息  
		/**
		 * @param {... } 
		 * @description 用户绑定咨询人
		 * @detail 
		 * @author lyy
		 */
		bindRelationUser: (data) => requesthttp('POST', `${lk_buyer}/buyer/passport/member/user/bindRelationUser`, data, {
			'content-type': 'application/x-www-form-urlencoded'
		}),
		/**
		 * @param {... } 
		 * @description 订阅 我的收藏
		 * @detail 
		 * @author lyy
		 */
		usercollect: (data) => requesthttp('POST', `${lk_buyer}/buyer/passport/member/user/collect`, data, {
			'content-type': 'application/x-www-form-urlencoded'
		}),
		/**
		 * @param {} 
		 * @description 查询用户绑定的咨询人
		 * @detail 
		 * @author lyy
		 */
		findRelationUser: (data) => requesthttp('POST', `${lk_buyer}/buyer/passport/member/user/findRelationUser`, data, {
			'content-type': 'application/json'
		}),
		/**
		 * @param {type,userId}   // false
		 * @description 我的订阅
		 * @detail 
		 * @author lyy
		 */
		subscribe: (data) => requesthttp('GET', `${lk_buyer}/buyer/passport/member/user/subscribe`, data, {
			'content-type': 'application/x-www-form-urlencoded'
		}),
		// 资讯管理接口
		/**
		 * @param { }   
		 * @description 
		 * @detail 
		 * @author lyy
		 */
		informationlist: (data) => requesthttp('GET', `${lk_expert}/information/list`, data, {
			'content-type': 'application/x-www-form-urlencoded'
		}),
		/**
		 * @param {"expertId": 0,"keyword": "","page": 0,"rows": 0,"sortField": "","sortType": "","type": 0}   // 2011-2014,2015其他
		 * @description 资讯分类展示关键字搜索
		 * @detail 
		 * @author lyy
		 */
		informationPagelist: (data) => requesthttp('POST', `${lk_expert}/information/pagelist`, data, {
			'content-type': 'application/json'
		}),
		/**
		 * @param { }   
		 * @description 
		 * @detail 
		 * @author lyy
		 */
		informationDetail: (id,data) => requesthttp('GET', `${lk_expert}/information/${id}`, data, {
			'content-type': 'application/x-www-form-urlencoded'
		}),
		
		
		//未使用-待定
		/**
		 * @param {  }
		 * @description 获取专家处方集合
		 * @detail 
		 * @author lyy
		 */
		// expertPrescriptionlist: (data) => requesthttp('GET', `${url1}/expertPrescription/list`, data, {
		// 	'content-type': 'application/x-www-form-urlencoded'
		// }),
		/**
		* @param {
				  "keyword": "",
				  "page": 0,
				  "rows": 0,
				  "sortField": "",
				  "sortType": ""
				}
		* @description 带条件分页查询数据
		* @detail 
		* @author lyy
		*/
		// prescriptionPagelist: (data) => requesthttp('GET', `${url1}/expertPrescription/pagelist`, data, {
		// 	'content-type': 'application/x-www-form-urlencoded'
		// }),
}
